<?php
namespace App\Http\Middleware;

use App\Models\AdminUser;
use App\Models\Capital;
use Closure;
use App\Utils\JwtTool;
use Illuminate\Http\Request;
use App\Utils\Response as UtilsResponse;
use Symfony\Component\HttpFoundation\Response;

class LoginAuthH5
{

    use UtilsResponse;
    /**
     * 验证登录
     *
     * @param Closure(Request): (Response) $next
     */
    public function handle(Request $request, Closure $next): Response
    {
        $token = $request->header("x-token","");
        if(empty($token)) return $this->httpError("登录失效",402);
        $tokenData = JwtTool::verifyToken($token);
        if(empty($tokenData)) return $this->httpError("登录失效",402);
        $user = Capital::query()->find($tokenData["id"]);
        if(empty($user)) return $this->httpError("登录失效",402);
        $request->offsetSet("userInfo",$user);
        return $next($request);
    }
}
